package com.magicborrow.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.magicborrow.R;
import com.magicborrow.adapter.ListFragmentAdapter;
import com.magicborrow.utils.UIHelper;
import java.util.ArrayList;

/**
 * Created by River on 5/17/16.
 */
public class SearchResultFragment extends Fragment implements View.OnClickListener{

    private RelativeLayout mFragmentLayout;
    private ViewPager mViewPager;
    private ListFragmentAdapter adapter;
    private ArrayList<Fragment> fragments;
    private TextView mBigShoot;
    private TextView mAnswer;
    private TextView mDreamGirl;
    private ImageView mTabLine;
    private int mScreen1_3;
    private int mCurrentPageIndex;
    SearchResultFilterFragment fragment1;
    SearchResultFilterFragment fragment2;
    SearchResultFilterFragment fragment3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mFragmentLayout = (RelativeLayout)inflater.inflate(R.layout.search_result_fragment,null);
        initView();
        initListener();
        return mFragmentLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView(){
        mViewPager = (ViewPager)mFragmentLayout.findViewById(R.id.viewPager);
        mBigShoot = (TextView)mFragmentLayout.findViewById(R.id.bigShoot);
        mAnswer = (TextView)mFragmentLayout.findViewById(R.id.answer);
        mDreamGirl = (TextView)mFragmentLayout.findViewById(R.id.dreamGirl);
        mTabLine = (ImageView)mFragmentLayout.findViewById(R.id.tabLine);
        initTabLine();
    }

    private void initTabLine() {
        mTabLine = (ImageView) mFragmentLayout.findViewById(R.id.tabLine);
        mScreen1_3 = UIHelper.getScreenWidth(getActivity())/3;
        ViewGroup.LayoutParams lp = mTabLine.getLayoutParams();
        lp.width = mScreen1_3;
        mTabLine.setLayoutParams(lp);
    }

    private void initData(){
        adapter = new ListFragmentAdapter(getActivity().getSupportFragmentManager());
        fragments = new ArrayList<>();
        fragment1 = new SearchResultFilterFragment();
        fragment1.setArguments(getArguments());
        fragment2 = new SearchResultFilterFragment();
        fragment2.setArguments(getArguments());
        fragment3 = new SearchResultFilterFragment();
        fragment3.setArguments(getArguments());
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        adapter.setData(fragments);
        mViewPager.setAdapter(adapter);
    }

    private void initListener(){
        mBigShoot.setOnClickListener(this);
        mAnswer.setOnClickListener(this);
        mDreamGirl.setOnClickListener(this);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPx) {
                LinearLayout.LayoutParams lp =
                        (LinearLayout.LayoutParams) mTabLine.getLayoutParams();
                //0->1
                if (mCurrentPageIndex == 0 && position == 0) {
                    lp.leftMargin = (int) (positionOffset * mScreen1_3 + mCurrentPageIndex* mScreen1_3);
                }
                //1->0
                else if (mCurrentPageIndex == 1 && position == 0) {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1)* mScreen1_3);
                }
                //1->2
                else if (mCurrentPageIndex == 1 && position == 1) {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + positionOffset* mScreen1_3);
                }
                // 2->1
                else if (mCurrentPageIndex == 2 && position == 1) {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1)
                            * mScreen1_3);
                }
                mTabLine.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int i) {
                mCurrentPageIndex = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

}
